#include <cstdio>
#include <unordered_set>
using namespace std;
/*
 * @lc app=leetcode.cn id=202 lang=cpp
 *
 * [202] 快乐数
 */

// @lc code=start
class Solution {
public:
    bool isHappy(int n) {
        unordered_set<int> used;
        while (n != 1) {
            n = get(n);
            if (used.count(n)) return false;
            used.insert(n);
        }

        return true;
    }

    int get(int n) {
        int res = 0;
        while (n) {
            int i = n % 10;
            res += i * i;
            n /= 10;
        }
        return res;
    }
};
// @lc code=end

